﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using System;
using com.guojin.mj.net.message.game;
/* ***********************************************
 * Describe: 录音WebSocket
 * Author : 赵中阳
 * Email: 1148312315@qq.com
 * DATA: 2017/7/28 16:00:21 
 * FileName: ocordWebSocket
 * Version: V1.0.1
 * ***********************************************/
public class MicSendMessagePage : MonoBehaviour
{
    private static MicSendMessagePage _instance = null;
    private MicSendMessagePage() { }
    public static MicSendMessagePage GetInstance()
    {
        if (_instance == null)
        {
            _instance = new MicSendMessagePage();
        }
        return _instance;
    }

    private string ServerAdress = "http://121.40.90.67:8092/up";
    //这个是收到1.25   4类型代表语音
    public int msgType = 4;

    void Start()
    {
    }

    //发送录音文件字节流，将返回字节流广播出去
    public void Send(byte[] data)
    {
        string voiceStr = Convert.ToBase64String(data);
        string param = "{ \"type\":\"ogg\",\"data\":\"data:audio/x-wav;base64," + voiceStr + "\"}";

        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("data", param);
        string result = HttpPostData(ServerAdress, dic);

        //服务器返回语音地址
        Debug.Log("服务器返回地址:" + result);

        MessageManager.GetInstance().SendMsg1224(result, msgType);
    }


    //发送录音字节流 收到服务器返回网址
    public string HttpPostData(string url, Dictionary<string, string> dic)
    {
        var result = string.Empty;

        //#region 添加Post 参数  
        StringBuilder builder = new StringBuilder();
        int i = 0;
        foreach (var item in dic)
        {
            if (i > 0)
                builder.Append("&");
            builder.AppendFormat("{0}={1}", item.Key, item.Value);
            i++;
        }

        byte[] postData = Encoding.UTF8.GetBytes(builder.ToString());
        // 设置提交的相关参数 

        System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        Encoding myEncoding = Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.ContentLength = postData.Length;
        request.Timeout = 3000;

        // 提交请求数据 
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response;
        Stream responseStream;
        StreamReader reader;
        string srcString;
        response = request.GetResponse() as System.Net.HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
        srcString = reader.ReadToEnd();
        result = srcString;   //返回值赋值
        reader.Close();
        Debug.Log("获取HttpPostData服务器返回Version" + result);
        return result;
    }

    void Update()
    {
    }
}